package com.yunxi.module.system.service.member.impl;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ReflectUtil;
import com.yunxi.framework.common.util.spring.SpringUtils;
import com.yunxi.module.system.service.member.MemberService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * <p>
 * Member Service 接口实现类
 * </p>
 *
 * @author lidy
 * @since 2024-05-08
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Value("${yunxi.info.base-package}")
    private String basePackage;

    private volatile Object memberUserApi;

    /**
     * 获得会员用户的手机号码
     *
     * @param userId 会员用户ID
     * @return 手机号码
     */
    @Override
    public String getMemberUserMobile(Long userId) {
        Object user = getMemberUser(userId);
        if (user == null) {
            return null;
        }
        return ReflectUtil.invoke(user, "getMobile");
    }

    /**
     * 获得会员用户信息
     *
     * @param userId 会员用户ID
     * @return 会员信息
     */
    private Object getMemberUser(Long userId) {
        if (userId == null) {
            return null;
        }

        return ReflectUtil.invoke(getMemberUserApi(), "getUser", userId);
    }

    /**
     * 获取会员用户的接口
     *
     * @return 会员信息
     */
    private Object getMemberUserApi() {
        if (memberUserApi == null) {
            memberUserApi = SpringUtils.getBean(ClassUtil.loadClass(String.format("%s.module.member.api.user.MemberUserApi", basePackage)));
        }
        return memberUserApi;
    }
}
